/*
 * Copyright (c) 2023-2025 110476592@qq.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * @file screenshot_toolbar.cpp
 * @brief 截图工具 
 * @author 110476592@qq.com 
 * @note   承接Qt项目外包定制开发。长期接收C++项目实训学徒,联系QQ：110476592。
 */

 
#include <common/global.h>
#include "screenshot_toolbar.h"
#include <QToolButton>
#include <QAction>

ScreenshotToolBar::ScreenshotToolBar(QWidget *parent)
    : AbsFloatToolBar{parent}
{
    setObjectName("ScreenshotToolBar");
}

void ScreenshotToolBar::initButtons()
{

    QToolButton *btn = addToolButton(tr("关闭"),(int)IconFont::FA_CLOSE,CommandType::CMD_CLOSE);
                                           m_buttonGroup->addButton(btn);
    m_hLayout->addWidget(btn);
    QString closeStyle = QString::fromUtf8(
       "QToolButton{background-color:#C84A46;padding:2px; color:#666;border: 1px solid #C84A46; }"
       "QToolButton:hover {"
       "border-radius:2px;"
       "}");

    btn->setStyleSheet(closeStyle);

    btn = addToolButton(tr("画笔"),(int)IconFont::FA_PEN,CommandType::CMD_PEN);
    m_buttonGroup->addButton(btn);
    m_hLayout->addWidget(btn);

    btn = addToolButton(tr("形状"),(int)IconFont::FA_SHAP,CommandType::CMD_SHAP);
    m_buttonGroup->addButton(btn);
    m_hLayout->addWidget(btn);
    btn = addToolButton(tr("撤销"),(int)IconFont::FA_UNDO,CommandType::CMD_UNDO);
    m_buttonGroup->addButton(btn);
    m_hLayout->addWidget(btn);

    btn = addToolButton(tr("重做"),(int)IconFont::FA_REDO,CommandType::CMD_REDO);
    m_buttonGroup->addButton(btn);
    m_hLayout->addWidget(btn);

    btn = addToolButton(tr("剪切板"),(int)IconFont::FA_CLIPBOARD,CommandType::CMD_CLIPBOARD);
    m_buttonGroup->addButton(btn);
    m_hLayout->addWidget(btn);

    btn = addToolButton(tr("保存文件"),(int)IconFont::FA_SAVE,CommandType::CMD_SAVE);
                                           m_buttonGroup->addButton(btn);
                           m_hLayout->addWidget(btn);

    QSize newPreferredSize = preferredSize();
    resize(newPreferredSize.width(), newPreferredSize.height());
}

void ScreenshotToolBar::updatePosition(const QRect &parentRect, const QRect &selectionRect)
{
    int top = parentRect.top();
    QRect rc = this->rect();
    if(selectionRect.top() - rc.height() > top )//选区上方空间足够显示工具栏
    {
        setStyleSheet(
            "ScreenshotToolBar{padding:10px;background-color:rgba(237, "
            "237, 237,125);border-top-left-radius:4px;border-top-right-radius:4px;}");
        move(selectionRect.left(),selectionRect.top() - rc.height());
        return ;
    }

    int bottom = parentRect.bottom();

    if(selectionRect.bottom() + rc.height() < bottom )//选区下方空间足够显示工具栏
    {
        setStyleSheet(
            "ScreenshotToolBar{padding:10px;background-color:rgba(255, "
            "255, 255,125);border-bottom-left-radius:4px;border-bottom-right-radius:4px;}");
        move(selectionRect.left(),selectionRect.bottom());
        return ;
    }

    move(selectionRect.left(),selectionRect.bottom() - rc.height());
}


